<?php
require_once __DIR__ . '/AdminController.php';

use \LeanCloud\Object;
use \LeanCloud\Query;
use \LeanCloud\File;

class Banner extends AdminController {
	function __construct() {
		parent::__construct();
		//$this->load->model('Category_model', 'category_model');
	}

	// 分类列表
	public function index() {

        $query = new Query("Banner");
        $result = $query->find();
        // 渲染
        $data['result'] = $result;
		$this->layout->view('banner/index', $data);
	}

	// 添加分类
	public function add() {

		$this->layout->view('banner/add');
	}

	// 保存分类
	public function save() {


		// banner图片上传
		if (!empty($_FILES['image']['tmp_name'])) {
			$banner = File::createWithLocalFile($_FILES['image']['tmp_name'], $_FILES['image']['type']);
			// 保存图片
			$banner->save();
			// banner图
		}
		// save to leanCloud
		$object = new Object("Banner");
		// 获取参数
		$proId = $this->input->post('proId');
		$object->set("proId", $proId);
		if (isset($banner)) {
			$object->set("banner", $banner);
		}
		// 提示信息 
		$data['redirect'] = 'index';
		try {
			$object->save();
			$data['msg'] = '保存成功';
			$data['level'] = 'info';
			$this->layout->view('banner/msg', $data);
		} catch (Exception $ex) {
			$data['msg'] = '操作失败';
			$data['level'] = 'warning';
			var_dump($ex);
		} finally {
			$this->layout->view('banner/msg', $data);
		}
	}

	// 删除分类
	public function delete() {
        $objectId = $this->input->get('objectId');
        $goods = Object::create('Banner', $objectId);
        $goods->destroy();
        $data['msg'] = '删除成功';
        $data['level'] = 'info';
        $data['redirect'] = 'index';
        $this->layout->view('banner/msg', $data);
	}
}
